package com.bbs2.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.bbs2.entity.Board;
import com.bbs2.util.DBHelp;
import com.bbs2.util.RowMapper;

public class BoardDao {
	private DBHelp<Board> db = new DBHelp<Board>();
	public List<Board> findAll(){
		String sql = "SELECT id,NAME,flag FROM t_board";
		return db.queryForList(sql, new BoardMap());
	}
	public Board findById(String  id){
		String sql = "SELECT id,NAME,flag FROM t_board WHERE id=?";
		return db.queryForObject(sql, new BoardMap(), id);
	}
	public int save(Board board){
		String sql = "INSERT INTO t_board(NAME,flag) VALUES(?,?);SELECT @@identity";
		return db.executeUpdate(sql, board.getName(),board.getFlag());
	}
	public int del(String id){
		String sql = "DELETE FROM t_board WHERE id=?";
		return db.executeUpdate(sql, id);
	}
	public int update(Board board){
		String sql = "UPDATE t_board SET NAME=?,flag=? WHERE id=?";
		return db.executeUpdate(sql, board.getName(),board.getFlag(),board.getId());
	}
}
class BoardMap implements RowMapper<Board>{

	public Board mapRow(ResultSet rs) throws SQLException {
		Board b = new Board();
		b.setId(rs.getInt("id"));
		b.setName(rs.getString("name"));
		b.setFlag(rs.getInt("flag"));
		return b;
	}
	
}